package com.sinux.generality.provider.demo.interceptor;

import com.sinux.generality.provider.demo.enums.WhiteListEnum;
import com.sinux.generality.provider.demo.util.JwtUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>LoginInterceptor
 *
 * @author Jasper
 * @version 1.0
 * @date 2023-05-07
 */

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 处理接口白名单
        if (request.getRequestURL() != null){
            String url = request.getRequestURL().toString();
            for (WhiteListEnum whiteListEnum : WhiteListEnum.values()) {
                if (url.contains(whiteListEnum.getUrl())){
                    return true;
                }
            }
        }

        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return false;
        }

        return JwtUtils.checkToken(token);

    }
}
